package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.Comment;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CommentService;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("CommentController")
public class CommentController {

    @Reference
    private CommentService commentService;

    @RequestMapping("findAll")
    @ResponseBody
    @CrossOrigin//解决跨域注解
    public List<Comment> findAll(String goodsId){
        List<Comment> commList = JSONObject.parseObject(commentService.findAll(goodsId),List.class);
        return commList;
    }
    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(String content,String img,String level,Long itemId){
//        content:$("#content").val(),img:$("#photo").val(),level:$("#level").val(),id:$("#id1").val()
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String itemStr = itemId + "";
        commentService.addInfo(content,img,level,itemStr,user.getUsername());
        return Result.response(ResultEnum.SUCCESS);
    }
}
